/**
 * 获取当前节点
 * @param {*} canvasObj
 * @param {*} currentNodeTag
 * @returns
 */
function getCurrentNode(canvasObj, currentNodeTag) {
	let nodes = canvasObj.find(currentNodeTag);
	// console.log(currentNodeTag, nodes, "当前节点");
	if (!nodes) {
		console.error("无指定的tag:" + currentNodeTag);
		return;
	}
	if (currentNodeTag && currentNodeTag.endsWith("-e")) {
		if (nodes.length > 1) {
			// throw new Error("配置文件出错， 只能有一个tag：" + currentNodeTag);
			console.error("配置文件出错， 只能有一个tag：" + currentNodeTag);
			return null;
		}
	}
	return nodes;
}

function handleLineData(res) {
	// 将新版本中的连接线的起始点和终点转为from和to（适应老版本编辑器的处理逻辑）
	res.pens &&
		res.pens.forEach((pen, index) => {
			if (pen.tags.includes("lcline")) {
				pen.anchors &&
					pen.anchors.forEach((anchor) => {
						if (anchor.start) {
							res.pens[index].from = {
								id: anchor.connectTo,
							};
						} else {
							res.pens[index].to = {
								id: anchor.connectTo,
							};
						}
					});
			}
		});
	return res;
}
/**
 * 获取节点相关的后续连线对象
 * @param {*} canvasObj
 * @param {*} currentNodeId
 * @param {*} type
 * @returns lines
 */
function getNextLineObj(canvasObj, currentNodeId, type = "lcline") {
	let lines = canvasObj.find(type);
	let relativeLines = lines.filter(
		(item) => item.from && item.from.id == currentNodeId
	); // 老版本乐吾乐
	return relativeLines;
	// // 返回后续连线的ID
	// let relativeLineIds = relativeLines.map(item => item.id);
	// return relativeLineIds;
}

function rendChangeNode(canvasObj, nodeTag, styleObj = {}) {
	let styleDefault = {
		locked: 1,
		background: "transparent",
		color: "#fff",
		textColor: "#fff",
	};
	// canvasObj &&
	//   canvasObj.setValue(nodeTag, {
	//     ...styleDefault,
	//     ...styleObj,
	//   }); // 老版本处理方式
	canvasObj &&
		canvasObj.setValue({
			id: nodeTag,
			...styleDefault,
			...styleObj,
		});
	canvasObj &&
		canvasObj.setValue({
			tag: nodeTag,
			...styleDefault,
			...styleObj,
		});

	// 处理应急中心车辆上没有信息框的问题
	if ("image" in styleObj) {
		if (styleObj["image"]) {
			let node = canvasObj.find(nodeTag);
			if (!node) return;
			let imageMaskObj = node.clone();
			imageMaskObj.image = "";
			imageMaskObj.stroke = "transparent";
			imageMaskObj.fill = "transparent";
			// imageMaskObj.tags.push("carpos_mask");
			imageMaskObj.tags = ["carpos_mask"];
			imageMaskObj.id = imageMaskObj.id + "_mask";
			canvasObj.addNode(imageMaskObj);
		} else {
			if (nodeTag === "carpos") {
				let allImageMaskObjs = canvasObj.find("carpos_mask");
				canvasObj.delete(allImageMaskObjs);
			} else {
				let allImageMaskObj = canvasObj.find(`${nodeTag}_mask`);
				canvasObj.delete(allImageMaskObj);
			}
		}
	}
}

/**
 * 获取节点相关的前续连线对象
 * @param {*} canvasObj
 * @param {*} currentNodeId
 * @param {*} type
 * @returns
 */
function getPrevLineObj(canvasObj, currentNodeId, type = "lcline") {
	let lines = canvasObj.find(type);
	// 获取当前点关联的前续连线
	let relativeLines = lines.filter((item) => item.to.id == currentNodeId);
	return relativeLines;
}





/**
 * 获取特定的节点以及后续相关连线对象
 * @param {*} canvasObj
 * @param {*} currentNodeTag
 * @returns
 */
function getSpecifiedItems(canvasObj, currentNodeTag) {
	let specifiedItem = getCurrentNode(canvasObj, currentNodeTag);
	let specifiedId =
		specifiedItem &&
		(specifiedItem.id || (specifiedItem[0] && specifiedItem[0].id));
	let specifiedRelativedItems = getNextLineObj(canvasObj, specifiedId);
	return {
		specifiedItem,
		specifiedRelativedItems,
	};
}


/**
 * 渲染状态
 * @param {*} canvasObj
 * @param {*} allItemsObj
 * @param {*} statusToColors
 */
function renderStatus(canvasObj, allItemsObj, statusToColors) {
	let keys = Object.keys(allItemsObj);
	// console.log(allItemsObj, "renderStatus");
	keys.forEach((key) => {
		let currentNode = getCurrentNode(canvasObj, key);
		currentNode = (Array.isArray(currentNode) && currentNode[0]) || currentNode;
		if (Array.isArray(allItemsObj[key])) {
			allItemsObj[key] = allItemsObj[key][0];
		}
		allItemsObj[key].status = allItemsObj[key].status || 0;
		let style1 = JSON.parse(
			JSON.stringify(statusToColors[allItemsObj[key].status])
		);
		// console.log(allItemsObj[key].type, allItemsObj[key], "allItemsObj[key]");
		if (allItemsObj[key].type == "line") {
			delete style1.bkType;
			delete style1.background;
			delete style1.gradientFromColor;
			delete style1.gradientToColor;
		}
		// console.log(key, allItemsObj[key].status, "changeNode");
		rendChangeNode(canvasObj, key, {
			...style1,
			textColor: allItemsObj[key].status == null &&
				allItemsObj[key].status == 0 &&
				allItemsObj[key].type == "line" ?
				"grey" :
				allItemsObj[key].status &&
				statusToColors[allItemsObj[key].status] &&
				statusToColors[allItemsObj[key].status]["textColor"],
		});
		if (allItemsObj[key].type == "line") {
			if (allItemsObj[key].status > 0) {
				canvasObj.top(currentNode);
			} else {
				canvasObj.down(currentNode);
			}
		}
	});
}






function clickLine(canvasObj, lineObj) {
	// 获取后续节点， 以及后续节点的相关线和相关节点
	let currentLine = lineObj;
	let prevNode =
		lineObj &&
		lineObj.from &&
		lineObj.from.id &&
		canvasObj.find(lineObj.from.id); // 前序节点
	let nextNode =
		lineObj && lineObj.to && lineObj.to.id && canvasObj.find(lineObj.to.id); // 后续节点
	let nextNodeId = nextNode && (nextNode.id || (nextNode[0] && nextNode[0].id));
	let nextLines = getNextLineObj(canvasObj, nextNodeId); // 后续线段
	let {
		speciLastItem
	} = getSpeciLastItems(canvasObj, "lcend"); //获取最后一个
	return {
		currentLine,
		prevNode,
		nextNode,
		nextLines,
		lastNode: speciLastItem,
	};
}
 function getSpeciLastItems(canvasObj, currentNodeTag) {
  let speciLastItem = getCurrentNode(canvasObj, currentNodeTag);
  return {
    speciLastItem,
  };
}